//
//  ClassVideoDetailViewController.m
//  MIDI
//
//  Created by E.M.Game on 15/9/11.
//  Copyright (c) 2015年 QXSX. All rights reserved.
//

#import "ClassVideoDetailViewController.h"
#import "AppConfig.h"
#import "VideoDetailCellTableViewCell.h"
#import "OpenClassVideoViewController.h"
#import "UIViewController+Create.h"
#import "OpenClassDetailModel.h"
#import "APIAdress.h"
#import "UIViewController+NavItem.h"
#import "UIViewController+NavItem.h"
#import "QXNetWorkTool.h"
#import "MBProgressHUD.h"
#import "MBProgressHUD+YC.h"
#import "APIAdress.h"
#import "UIImageView+AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "CommFunc.h"
#import "MJRefresh.h"
#import "NewVideoDetailCellTableViewCell.h"

@interface ClassVideoDetailViewController ()<UITableViewDataSource , UITableViewDelegate , QXNetWorkToolDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong , nonatomic) QXNetWorkTool *netTool;
@property (strong , nonatomic) NSMutableArray *classData;
@property (assign , nonatomic) NSInteger pageNumbers;    //持有多少页的数据
@property (strong , nonatomic) NewVideoDetailCellTableViewCell *myCell;
@end

@implementation ClassVideoDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.netTool = [[QXNetWorkTool alloc]init];
    self.netTool.QXNetWorkDelegate = self;
    self.pageNumbers = 1;
    self.classData = [[NSMutableArray alloc]init];
    
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    
    self.tableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    [self reqData];
}

#pragma mark 设置上下拉刷新

- (void)loadNewData
{
    [self.classData removeAllObjects];
    self.pageNumbers = 1;
    
    [self reqData];
    [self.tableView.header endRefreshing];
}

- (void)loadMoreData
{
    ++self.pageNumbers;
    
    [self reqData];
    [self.tableView.footer endRefreshing];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self configNavigationItem];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



-(void) configNavigationItem
{
    [self.tabBarController configNavigationBarTintColor:UIColorRGBA(0, 0, 0, 1)];
    [self.navigationController.navigationBar setBarTintColor:MIDIColorOfNavigationBar];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.navigationItem.title = @"公开课视频";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor, [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0],UITextAttributeTextShadowColor,[UIFont fontWithName:@"Hiragino Sans GB" size:18.0],UITextAttributeFont,nil]];
    [self configNavigationBackAction:^{
        
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
}

-(void)reqData
{
    [self.netTool GetDataFromServerWithPostMethodandInterfaceName:OpenClassListInterface andParameter:@[@{@"nowpage":[NSString stringWithFormat:@"%ld" , (long)self.pageNumbers] , @"pagesize":@"10"}] tag:1];
    
    [MBProgressHUD showMessage:@"正在加载" toView:self.view];
}

#pragma mark QxNetTool

-(void)GetSuccessResponse:(id)Response andTag:(int)tag
{
    [MBProgressHUD hideHUDForView:self.view];
    
    if(tag == 1)
    {
        int i = 0;
        for(NSDictionary * dic in Response)
        {
            OpenClassDetailModel * item = [OpenClassDetailModel MyModelWithDict:dic];
            [self.classData addObject:item];

        }
        [self.tableView reloadData];
    }
}

- (void)GetSuccessMessage:(id)message andTag:(int)tag
{
    NSString *mes = message;
    
    switch (tag)
    {
        case 1:
            [MBProgressHUD showSuccess:mes toView:self.view];
            break;
    }
}

- (void)GetFailResponse:(id)Response andTag:(int)tag
{
    [MBProgressHUD hideHUDForView:self.view];
    
    if (Response == nil)
        return;
    
    NSString *message = Response;
    
    [MBProgressHUD showError:message toView:self.view];
    
}

#pragma mark tableView datasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([self.classData count] % 2 == 0)
    {
        return [self.classData count] / 2;
    }
    
    return [self.classData count] / 2 + 1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if (self.myCell)
//    {
//        return [self.myCell getHeight];
//    }
    
    return 160;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.classData count] > 0)
    {
        NSString * identifier = @"NewVideoDetailCellTableViewCell";
        self.myCell = [self.tableView dequeueReusableCellWithIdentifier: identifier];
        if(self.myCell == nil)
        {
            self.myCell = [NewVideoDetailCellTableViewCell loadFromNib];
        }
        [self configCell:indexPath.row cell:self.myCell];
        
        return self.myCell;
    }
    else
    {
        UITableViewCell *cell = [[UITableViewCell alloc]init];
        return cell;
    }
}

#pragma mark - Table View Delegate
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    if ([self.classData count] > 0)
//    {
//        [self openClassVideo:indexPath.row];
//    }
//}


-(void)configCell : (NSInteger)row cell:(NewVideoDetailCellTableViewCell *) myCell
{
    if ([self.classData count] > 0)
    {
        if (([self.classData count] - row * 2) == 1)
        {
            [self setCellFirstView:row cell:myCell];
        }
        else
        {
            if ([self.classData count] - 1 >= row * 2)
                [self setCellFirstView:row cell:myCell];
            
            if ([self.classData count] - 1 >= row * 2 + 1)
                [self setCellSecondView:row cell:myCell];
        }
    }
}

- (void)setCellFirstView:(NSInteger)row cell:(NewVideoDetailCellTableViewCell *) myCell
{
    OpenClassDetailModel *data;
    
    data = [self.classData objectAtIndex:row * 2];
    
    if (data)
    {
        myCell.viewContent.hidden = NO;
        [myCell.videoTitle setText:data.lesson_name];
        [myCell.videoDescLabel setText:data.lesson_brief];
        
        if (data.lesson_icon != nil && ![data.lesson_icon isEqualToString:@""])
        {
            [myCell.videoImg sd_setImageWithURL:[NSURL URLWithString:data.lesson_icon]];
        }
        
        myCell.viewContent.tag = row * 2;
        
        UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
        
        [myCell.viewContent addGestureRecognizer:tapGestureRecognizer];
    }
}

- (void)setCellSecondView:(NSInteger)row cell:(NewVideoDetailCellTableViewCell *) myCell
{
    OpenClassDetailModel *data;
    data = [self.classData objectAtIndex:row * 2 + 1];
    
    if (data)
    {
        myCell.secondViewContent.hidden = NO;
        [myCell.secondVideoTitle setText:data.lesson_name];
        [myCell.secondVideoDescLabel setText:data.lesson_brief];
        
        if (data.lesson_icon != nil && ![data.lesson_icon isEqualToString:@""])
        {
            [myCell.secondVideoImg sd_setImageWithURL:[NSURL URLWithString:data.lesson_icon]];
        }
        
        myCell.secondViewContent.tag = row * 2 + 1;
        
        UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
        
        [myCell.secondViewContent addGestureRecognizer:tapGestureRecognizer];
    }
}


//- (void)clickVideoBt : (UIButton *)sender
//{
//    [self openClassVideo:sender.tag];
//}
//
- (void)tap:(UITapGestureRecognizer *) sender
{
    [self openClassVideo:sender.view.tag];
}

- (void)openClassVideo:(NSUInteger)tag
{
    OpenClassVideoViewController *ctr =[OpenClassVideoViewController create];
    OpenClassDetailModel *data = [self.classData objectAtIndex:tag];
    [ctr setConfigData:data.id];
    [self.navigationController pushViewController:ctr animated:YES];
}

@end


/*
@interface ClassVideoDetailViewController ()<UITableViewDataSource , UITableViewDelegate , QXNetWorkToolDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong , nonatomic) QXNetWorkTool *netTool;
@property (strong , nonatomic) NSMutableArray *classData;
@property (assign , nonatomic) NSInteger pageNumbers;    //持有多少页的数据
@property (strong , nonatomic) VideoDetailCellTableViewCell *myCell;
@end

@implementation ClassVideoDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.netTool = [[QXNetWorkTool alloc]init];
    self.netTool.QXNetWorkDelegate = self;
    self.pageNumbers = 1;
    self.classData = [[NSMutableArray alloc]init];
    
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    
    self.tableView.footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    
    [self reqData];
}

#pragma mark 设置上下拉刷新

- (void)loadNewData
{
    [self.classData removeAllObjects];
    self.pageNumbers = 1;
    
    [self reqData];
    [self.tableView.header endRefreshing];
}

- (void)loadMoreData
{
    ++self.pageNumbers;
    
    [self reqData];
    [self.tableView.footer endRefreshing];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self configNavigationItem];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



-(void) configNavigationItem
{
    [self.tabBarController configNavigationBarTintColor:UIColorRGBA(0, 0, 0, 1)];
    [self.navigationController.navigationBar setBarTintColor:MIDIColorOfNavigationBar];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    self.navigationItem.title = @"公开课视频";
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],UITextAttributeTextColor, [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0],UITextAttributeTextShadowColor,[UIFont fontWithName:@"Hiragino Sans GB" size:18.0],UITextAttributeFont,nil]];
    [self configNavigationBackAction:^{
        
        [self.navigationController popViewControllerAnimated:YES];
    }];

}

-(void)reqData
{
    [self.netTool GetDataFromServerWithPostMethodandInterfaceName:OpenClassListInterface andParameter:@[@{@"nowpage":[NSString stringWithFormat:@"%ld" , (long)self.pageNumbers] , @"pagesize":@"10"}] tag:1];
    
    [MBProgressHUD showMessage:@"正在加载" toView:self.view];
}

#pragma mark QxNetTool

-(void)GetSuccessResponse:(id)Response andTag:(int)tag
{
    [MBProgressHUD hideHUDForView:self.view];

    if(tag == 1)
    {
        for(NSDictionary * dic in Response)
        {
            OpenClassDetailModel * item = [OpenClassDetailModel MyModelWithDict:dic];
            [self.classData addObject:item];
        }
        [self.tableView reloadData];
    }
}

- (void)GetSuccessMessage:(id)message andTag:(int)tag
{
    NSString *mes = message;
    
    switch (tag)
    {
        case 1:
            [MBProgressHUD showSuccess:mes toView:self.view];
            break;
    }
}

- (void)GetFailResponse:(id)Response andTag:(int)tag
{
    [MBProgressHUD hideHUDForView:self.view];
    
    if (Response == nil)
        return;
    
    NSString *message = Response;
    
    [MBProgressHUD showError:message toView:self.view];
    
}

#pragma mark tableView datasource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.classData.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.myCell)
    {
        return [self.myCell getHeight];
    }
    
    return 24;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.classData count] > 0)
    {
        NSString * identifier = @"VideoDetailCellTableViewCell";
        self.myCell = [self.tableView dequeueReusableCellWithIdentifier: identifier];
        if(self.myCell == nil)
        {
            self.myCell = [VideoDetailCellTableViewCell loadFromNib];
        }
        [self configCell:indexPath.row cell:self.myCell];
        
//        self.myCell.tag = self.myCell.videoBtn.tag;
        
//        UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
//        
//        [self.myCell addGestureRecognizer:tapGestureRecognizer];
        
        return self.myCell;
    }
    else
    {
        UITableViewCell *cell = [[UITableViewCell alloc]init];
        return cell;
    }
}

#pragma mark - Table View Delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.classData count] > 0)
    {
        [self openClassVideo:indexPath.row];
    }
}


-(void)configCell : (NSInteger)row cell:(VideoDetailCellTableViewCell *) myCell
{
    if ([self.classData count] > 0)
    {
        myCell.videoBtn.tag = row;
        OpenClassDetailModel *data = [self.classData objectAtIndex:row];
        [myCell.videoBtn addTarget:self action:@selector(clickVideoBt:) forControlEvents:UIControlEventTouchUpInside];
        [myCell.videoTitle setText:data.lesson_name];
        [myCell.videoDescLabel setText:data.lesson_brief];
        
        if (data.lesson_icon != nil && ![data.lesson_icon isEqualToString:@""])
        {
            [myCell.videoImg sd_setImageWithURL:[NSURL URLWithString:data.lesson_icon]];
        }
    }
}

//- (void)clickVideoBt : (UIButton *)sender
//{
//    [self openClassVideo:sender.tag];
//}
//
//- (void)tap:(UITapGestureRecognizer *) sender
//{
//    [self openClassVideo:sender.view.tag];
//}

- (void)openClassVideo:(NSUInteger)tag
{
    OpenClassVideoViewController *ctr =[OpenClassVideoViewController create];
    OpenClassDetailModel *data = [self.classData objectAtIndex:tag];
    [ctr setConfigData:data.id];
    [self.navigationController pushViewController:ctr animated:YES];
}

@end

*/

















